var Hole = {
    holes:[],
    newHole:function(width,length,floor,id){
        if(this.holes[floor] == undefined) this.holes[floor] = [];
        this.holes[floor][id] = {
            width:width,
            length:length,
            people:[],
            floor:floor,
            id:id
        }
    },
    runHole:function(hole){
        let people = hole.people;
        let holeMaxX = hole.width;
        let holeMaxY = hole.length;
        for(let i in people){
            let peo = people[i];
            if(peo == null) continue;
            if(peo.y>holeMaxY) {
                people[i] = null;continue;//记得改
            }
            peo.x+=peo.vx;
            if(peo.x<1) peo.x=1;
            if(peo.x>holeMaxX-1)peo.x=holeMaxX-1;
            peo.y+=peo.vy; 
            // if(peo.vy<=1+Math.random()*0.5) peo.vy+=1;//驱动力
            //v1
            // for(let ii in people){
            //     if(i==ii) continue;
            //     peoI = people[ii];
            //     if(distance(peo,peoI) <2){//发生碰撞
            //             //peo.vx=(peoI.vx<0?1:-1)*Math.max(Math.abs(peo.vx),0.5);
            //             peo.vx=0;
            //             if(peo.y<peoI.y)peo.vy=0;
            //     }
            // }
            let accuracy = 64;
            let acuR = Math.PI/accuracy/2;
            let minsit=0;let minWt=doubleMax;
            for(let sitk =acuR;sitk<=Math.PI;sitk+=acuR){
                let res = 0;
                
                for(let ii in people){
                    let peoI = people[ii];
                    if(people[ii] == null) continue;
                    if(i == ii || peoI.y-1<= peo.y || ((distance(peo,peoI)>2))) continue;
                    //console.log(Math.abs(peo.x-peoI.x)<0.1);

                    res+=1/(Math.abs(sitaGiver(peoI.x-peo.x,peoI.y-peo.y)-sitk)*distance(peoI,peo));
                }
                res+=1/(Math.abs((Math.PI-sitk)*peo.x))*0.1;
                res+=1/(Math.abs((sitk)*(holeMaxX-peo.x)))*0.1;
                // console.log(i+" "+sitk+" "+res)
                if(res>=0 && res<minWt){
                    minsit = sitk;
                    minWt = res;
                }
                if(res ==0){//轨道修正
                    minsit = Math.PI/2;
                    minWt = Math.random()*0.5;
                    break;
                }
            }
            //Math.sqrt(peoI.vx*peoI.vx+peoI.vy*peoI.vy);
            let vabs = Math.min(Math.max(0.1/minWt,0.1),1);
            //console.log(minWt);
            let oldSita =sitaGiver(peo.vx,peo.vy);
            minsit = oldSita+(minsit-oldSita)*0.5;
            peo.vx = vabs*Math.cos(minsit);
            peo.vy = vabs*Math.sin(minsit);
            // if(peo.x<=1){
            //     peo.vx=0.5;
            // }
            // if(peo.x>=7){
            //     peo.vx=0.5;
            // }

        }
    },
    displayHole:function(hole,lefx,upy){
        canvas.fillStyle = "white";
        let people = hole.people;
        let holeMaxX = hole.width;

        // canvas.fillStyle = "red";
        // for(let i in people){
        //     if(people[i] == null) continue;
        //     canvas.beginPath();
        //     canvas.arc(lefx+wid*people[i].x/holeMaxX,upy+wid*people[i].y/holeMaxX,wid*1/holeMaxX+0.3,0,2*Math.PI);
        //     canvas.fill();
        // }
        canvas.fillStyle = "black";
        for(let i in people){
            if(people[i] == null) continue;
            canvas.beginPath();
            canvas.arc(lefx+wid*people[i].x/holeMaxX,upy+wid*people[i].y/holeMaxX,wid*1/holeMaxX,0,2*Math.PI);
            canvas.fill();
        }
        canvas.beginPath();
        drawLine(lefx,upy,lefx,upy+wid*holeMaxY*holeMaxX);
        drawLine(lefx+wid,upy,lefx+wid,upy+wid*holeMaxY*holeMaxX);

    }
}